package com.xdjk.networkdemo.activity;

import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.orhanobut.logger.Logger;
import com.xdjk.networkdemo.R;
import com.xdjk.networkdemo.adapter.ListAdapter;
import com.xdjk.networkdemo.base.BaseActivity;
import com.xdjk.networkdemo.listener.OnItemClickLitener;
import com.xdjk.networkdemo.listener.OnLoadMoreListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SlideActivity extends BaseActivity implements OnItemClickLitener, OnLoadMoreListener {


    @BindView(R.id.toolBar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    XRecyclerView mRecyclerview;
    @BindView(R.id.btn_top)
    FloatingActionButton btnTop;
    @BindView(R.id.bottom_navigation)
    BottomNavigationView bottomNavigation;
    @BindView(R.id.navigation)
    NavigationView navigation;
    @BindView(R.id.dl_root)
    DrawerLayout dlRoot;
    private ListAdapter mAdapter;
    //    private ArrayList<String> list = new ArrayList(Arrays.asList("测试数据1", "测试数据2","测试数据3","测试数据4","测试数据5","测试数据6","测试数据7","测试数据8","测试数据9","测试数据10"));
    private ArrayList<String> list = null;
    private SearchView mSearchView;
    private int countflag = 0;
    private int intervalLength = 30;
    private LinearLayoutManager linearLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_slide;
    }

    @Override
    protected void initParms(Bundle bundle) {
        setSwipeBackEnable(false);
    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < intervalLength; i++) {
            countflag++;
            list.add("测试数据" + i);
        }
    }

    @Override
    protected void initViews() {
        toolbar.setLogo(R.mipmap.ab_android);
        toolbar.setTitle("Title1");
        toolbar.setSubtitle("SubTitle1");
        //设置导航图标要在setSupportActionBar方法之后
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.ic_launcher_round);
        // Menu item click 的監聽事件一樣要設定在 setSupportActionBar 才有作用
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//这句代码使启用Activity回退功能，并显示Toolbar上的左侧回退图标

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, dlRoot, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        dlRoot.addDrawerListener(toggle);
        toggle.syncState();

        linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerview.setLoadingMoreProgressStyle(ProgressStyle.BallBeat);
        mRecyclerview.setLoadingMoreEnabled(true);
        mRecyclerview.setPullRefreshEnabled(true);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerview.setLayoutManager(linearLayoutManager);
        mRecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mAdapter = new ListAdapter(this, list);
        mRecyclerview.setAdapter(mAdapter);
        initNavs();
    }

    @Override
    protected void initListener() {
        mRecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                for (int i = 0; i < intervalLength; i++) {
                    countflag++;
                    list.add("测试数据" + i);
                }
                mRecyclerview.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                mRecyclerview.loadMoreComplete();
            }
        });
        mAdapter.setOnItemClickLitener(this);
        mAdapter.setOnLoadMoreListener(this);
    }

    private void initNavs() {
        navigation.setNavigationItemSelectedListener(item -> {
            String msg = "";
            switch (item.getItemId()) {
                case R.id.nav_camera:
                    msg += "Click nav_camera";
                    break;
                case R.id.nav_gallery:
                    msg += "Click nav_gallery";
                    break;
                case R.id.nav_slideshow:
                    msg += "Click nav_slideshow";
                    break;
                case R.id.nav_manage:
                    msg += "Click nav_manage";
                    break;
            }
            if (!msg.equals("")) {
                Toast.makeText(SlideActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
            dlRoot.closeDrawer(GravityCompat.START);
            return true;
        });

        bottomNavigation.setOnNavigationItemSelectedListener(item -> {
            String msg = "";
            switch (item.getItemId()) {
                case R.id.action_favorites:
                    msg += "Click home";
                    break;
                case R.id.action_schedules:
                    msg += "Click schedules";
                    break;
                case R.id.action_music:
                    msg += "Click music";
                    break;
            }
            if (!msg.equals("")) {
                Toast.makeText(SlideActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
            return true;    //这里返回true，表示事件已经被处理。如果返回false，为了达到条目选中效果，还需要下面的代码
            // item.setChecked(true);  不论点击了哪一个，都手动设置为选中状态true（该控件并没有默认实现)
            // 。如果不设置，只有第一个menu展示的时候是选中状态，其他的即便被点击选中了，图标和文字也不会做任何更改
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 监控返回键
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            if (dlRoot.isDrawerOpen(GravityCompat.START)) {
                dlRoot.closeDrawer(GravityCompat.START);
                return false;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onItemClick(View view, int position) {

    }

    @Override
    public void onLoadMoreData() {
        list = new ArrayList<>();
        for (int i = countflag; i < countflag + intervalLength; i++) {
            list.add("测试数据" + i);
        }
        mAdapter.notifyDataSetChanged();
        mRecyclerview.loadMoreComplete();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);//加载menu文件到布局
        final MenuItem item = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) MenuItemCompat.getActionView(item);
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Logger.d("onQueryTextSubmit=" + query);
                mSearchView.setQuery("", false);
                //可以收起键盘
                mSearchView.clearFocus();
                //可以收起SearchView视图
                mSearchView.onActionViewCollapsed();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return true;
            }
        });
        return true;
    }

    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            String msg = "";
            switch (menuItem.getItemId()) {
                case android.R.id.home:
                    msg += "Click home";
                    break;
                case R.id.action_edit:
                    msg += "Click edit";
                    break;
                case R.id.action_share:
                    msg += "Click share";
                    break;
                case R.id.action_search:
                    msg += "Click search";
                    break;
            }

            if (!msg.equals("")) {
                Toast.makeText(SlideActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    };

    @OnClick(R.id.btn_top)
    public void onClick() {
        Logger.d("点击回到顶部按钮");
        //listView返回到顶部
        mRecyclerview.smoothScrollToPosition(0);
    }
}
